<?php 
namespace My\Controller;

use Utuo\BackgroundController;

class CustomerProductGoodsController extends BackgroundController {
	
	protected $pid = 0;
	
	public function _initialize() {
		$this->pid = session("pid");
		$this->setControllerTitle('商品管理');
		$this->setActionTitle(array(
				'add' => '添加商品',
				'index' => '商品列表',
				'modify' => "编辑商品",
				'recycle' => '回收站',
		));
		$this->setActionNav('index,add,recycle');
	}
	
	public function getLists( $is_deleted = 0) {
		$page = $this->getPage();
		$pageSize = $this->getPageSize();
		$m = M("ProductGoods");
		$m->where(array('pid' => $this->pid, 'cid' => $this->aid, 'is_deleted' => $is_deleted));
		$n = clone $m;
		$totalRows = $n->count();
		$customerProductGoodsList = $m->page($page)->limit($pageSize)->select();
		$this->showPage($totalRows, $pageSize);
		$this->assign("customerProductGoodsList", $customerProductGoodsList);
		$this->display();
	}
	
	// 添加商品页面
	public function addAction() {
		$d = D("ProductGoods");
		$this->assign('is_shipping', $this->getRadio('is_shipping', $this->isShipping));
		$this->assign('priceAttributes', $this->getOption($d->priceAttributes, null, '==请选择=='));
		$this->assign('status', $this->getRadio('status', $this->status, 1));
		$this->display();
	}
	
	// 添加商品
	public function addPostAction() {
		$dProductGoods = D('ProductGoods');
		$dProductGoodsConfig = D('ProductGoodsConfig');
		if ($dProductGoods->create() && $dProductGoodsConfig->create()) {
			$dProductGoods->pid = $this->pid;
			$dProductGoods->cid = $this->aid;
			if ($id = $dProductGoods->add() and $dProductGoodsConfig->id = $id) {
				$dProductGoodsConfig->add() and $this->ajaxSuccess('添加商品成功');
				$this->ajaxError('添加商品失败');
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	// 修改商品
	public function modifyAction() {
		$this->resetHeader();
		$id = I("id", 0, "intval");
		$dProductGoods = D('ProductGoods');
		$productGoods = $dProductGoods->alias('this0')->join("__PRODUCT_GOODS_CONFIG__ this1 on this1.id = this0.id", 'LEFT')
									->join("__CUSTOMER__ this2 on this0.cid = this2.id", 'LEFT')
									->field("this0.*,this1.*,this2.name as customerName")
									->where("this0.id=$id and this0.pid={$this->pid}")->find();
		/*获得类目路径*/
		$classId = $productGoods['class_id'];
		$d = D('ProductClass');
		$path = "";
		while($classId) { $path=$classId.','.$path;	$class = $d->find($classId) and $classId = $class['pid']; }
		$dProductClass = D("Admin/ProductClass");
		/*品牌*/
		$brandString = $dProductClass->getClassBrand( $productGoods['class_id'] );
		if ( !empty( $brandString ) ) {
			$brand = M("ProductBrand")->where(array("id" => array("in", $brandString)))->getField("id,name");
		}
		/*功效*/
		$efficiencyString = $dProductClass->getClassEfficiency( $productGoods['class_id'] );
		if ( !empty( $efficiencyString ) ) {
			$efficiency = M("ProductEfficiency")->where(array("id" => array("in", $efficiencyString)))->getField("id,name");
		}
		$productGoods['brandOptions'] = $this->getOption($brand, $productGoods['brand'], '==请选择==');
		$productGoods['efficiencyCheckbox'] = $this->getCheckBox("efficiency[{:index}]", $efficiency, explode(',', $productGoods['efficiency']));
		/*获得类目所有属性*/
		$productGoods['classPath'] = trim($path, ",");
		$productGoods['is_low'] = $this->getRadio('is_low', $this->isLows, $productGoods['is_low']);
		$productGoods['status'] = $this->getRadio('status', $this->status, $productGoods['status']);
		$productGoods['is_shipping'] = $this->getRadio('is_shipping', $this->isShipping, $productGoods['is_shipping']);
		$productGoods['shipping'] = explode("|", $productGoods['shipping_cfg']);
		$productGoods['priceAttributes'] = $this->getOption($dProductGoods->priceAttributes, $productGoods['price_attribute'], '==请选择==');
		$this->assign("product", $productGoods);
		$this->display();
	}
	
	// 修改商品
	public function modifyPostAction() {
		$dProductGoods = D('ProductGoods');
		$dProductGoodsConfig = D('ProductGoodsConfig');
		if ($dProductGoods->create() and $configData = $dProductGoodsConfig->create()) {
			if($dProductGoods->class_id) {
				/*是否可选*/
				$productClass = M("ProductClass")->find($dProductGoods->class_id);
				$productClass['type'] != \Admin\Model\ProductClassModel::TYPE_ALWAYS_SELECTABLE and $productClass['has_child']!=0 and $this->ajaxError('当前选择的类目无效');
			}
			if (false !== $dProductGoods->where(array('pid'=>$this->pid))->save()) {
				(count($configData)==1 or false !== $dProductGoodsConfig->save()) and $this->ajaxSuccess();
				$this->ajaxError("修改商品失败");
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}	
	
	// 设置状态(0.下架 1.上架)
	public function setStatusAction() {
		$this->ajaxSetStatus('ProductGoods');
	}
	
	// 获取子类目
	public function classPostAction() {
		$id = I('id', 0, 'intval');
		$d = D('ProductClass');
		$data = $d->where(array('pid'=>$id, 'is_deleted'=> 0))->order('sort')->select();
		foreach($data as &$v) $v['isParent'] = true;
		$this->ajaxSuccess('', $data);
	}
	
}